package mysql

import (
	"fmt"
	"myweb/web/model"
	"strconv"
)

// 根据房屋ID，更新房屋图片
func UpdateHouseImage(housid, imageurl string) error {
	var houseinfo model.House
	if err := GormDB.Where("id = ?", housid).Find(&houseinfo).Error; err != nil {
		fmt.Println("查询不到房屋信息", err)
		return err
	}

	// 没有上传过照片，现在上传的是主图
	if houseinfo.Index_image_url == "" {
		return GormDB.Model(new(model.House)).Where("id=?", housid).Update("index_image_url", imageurl).Error
	}

	// 上传的副图，存入表house_image
	var houseimage model.HouseImage
	houseimage.Url = imageurl
	hid, _ := strconv.Atoi(housid)
	houseimage.HouseId = uint(hid)
	return GormDB.Create(&houseimage).Error
}
